library(plyr)
library(dplyr)
library(ggplot2)
library(RColorBrewer)
library(ggpubr)
library(wesanderson)
library(ggsignif)
library(tidyverse)
library(scales)
library(tidyr)
library(tibble)
library(openxlsx)
library(ggplot2)
library(ggpubr)
library(survminer)
library(survival)
library(Rcpp)
library(broom)
#change to the main folder containing your three folders: input, output, and R code
base_directory <- "C:/Users/Angela/Desktop/cox regression datasheets/Lifespan Assays Cox Regression/Carb and EDTA Replicates/Cox regression carb edta rep 2"
setwd(base_directory)
rcode_folder <- paste(base_directory, "R code", sep = "/")
input_folder <- paste(base_directory, "input", sep = "/")
output_folder <- paste(base_directory, "output", sep = "/")
data = read.csv(paste(input_folder, "cox regression Carb and EDTA rep 2.csv", sep = "/"), header = TRUE, sep = ',')
data <- na.omit(data)
names(data)[1] <- "condition"
data$technical.replicate <- "A"
data$replicate_group <- interaction(data$condition, data$technical.replicate)
#change RNAi and genotype to be the experimental variables in your data. If you have more than 2, you can add more than 2 to the list.
conditions <- c("Plate", "genotype")
source(file.path(rcode_folder, "reformat_data_include_rupture.R"))
source(file.path(rcode_folder, "reformat_data_censor_rupture.R"))
source(file.path(rcode_folder, "format_for_ggplot.R"))
source(file.path(rcode_folder, "log_rank_all_groups.R"))
source(file.path(rcode_folder, "basic_stats.R"))
reformatted <- reformat_data_include_rupture(data, conditions)
to_plot <- format_for_ggplot(reformatted, conditions)
write.csv(to_plot, paste(output_folder, "data_formatted_to_plot.csv", sep = "/"), row.names = FALSE)
#now run statistics on your data
#this function will give you mean lifespan, standard deviation, hazard ratios, and log-rank comparisons across all conditions
basic_stats(reformatted, output_folder)
subset <- reformatted
#set your control variables to be zero
subset[subset$Plate == "water",]$Plate <- 0
subset[subset$genotype == "WT",]$genotype <- 0
#then, create your cox regression model. To do this, replace RNAi and genotype with the conditions you want to look for an interaction between
cox <- coxph(Surv(time, censored) ~ Plate*genotype, data = subset)
summary(cox)
to_save <- tidy(cox)
to_save$exp_coef_haz_ratio <- exp(to_save$estimate)
#rename the text in quotes if you use a different subset
write.csv(to_save, paste(output_folder, "cox_regression_carb_edta_rep2.csv", sep ="/"), row.names = FALSE)
